{-# OPTIONS_GHC -O2 #-}

import Data.List
import Data.Maybe
import Data.Char
import Data.Array.IArray
import Data.Array.Unboxed (UArray)
import Data.Int
import Data.Ratio
import Data.Bits
import Data.Function
import Data.Ord
import Control.Monad.State
import Control.Monad
import Control.Applicative
import Data.ByteString.Char8 (ByteString)
import qualified Data.ByteString.Char8 as BS
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Map (Map)
import qualified Data.Map as Map
import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap
import Data.Sequence (Seq, (<|), (|>), (><), ViewL(..), ViewR(..))
import qualified Data.Sequence as Seq
import qualified Data.Foldable as F
import Data.Graph
import Control.Parallel.Strategies

import Debug.Trace

parseInput = do 
    cas <- readInt
    replicateM cas $ do
        names <- words . BS.unpack <$> readLine
        prices <- map read . words . BS.unpack <$> readLine
        return $ zip names prices
  where
    readInt = state $ fromJust . BS.readInt . BS.dropWhile isSpace
    readInteger = state $ fromJust . BS.readInteger . BS.dropWhile isSpace
    readString = state $ BS.span (not . isSpace) . BS.dropWhile isSpace
    readLine = state $ BS.span (not . isEoln) . BS.dropWhile isEoln
    isEoln ch = ch == '\r' || ch == '\n'

main = do
    input <- evalState parseInput <$> BS.getContents
    let output = parMap rdeepseq solve input
    forM_ (zip [1..] output) $ \(cas, result) -> do
        putStrLn $ "Case #" ++ show cas ++ ": " ++ result

longestIncreasingSubsequence :: Ord a => [a] -> Int
longestIncreasingSubsequence []  = 0
longestIncreasingSubsequence seq = maximum $ map snd seq'
  where
    merge xs y = (y, maximum (1 : [len + 1 | (x, len) <- xs, x < y])) : xs
    
    seq' = foldl merge [] seq

solve :: [(String, Int)] -> String
solve info = unwords $ go names info
  where
    names = sort $ map fst info
    prices = map snd info
    
    len = longestIncreasingSubsequence prices

    go [] xs = []
    go (name:names) xs
        | len' == len = name : go names xs'
        | otherwise   = go names xs
      where
        xs' = [x | x <- xs, fst x /= name]

        len' = longestIncreasingSubsequence (map snd xs')
